﻿using System;
using System.Windows.Input;
using System.Windows.Threading;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace mal.WP7.StopwatchTimer.ViewModels
{
    public class TimerViewModel : ViewModelBase
    {
        #region Fields
        private int hours = 0;
        private int minutes = 0;
        private int seconds = 0;
        private DateTime startDate;
        private TimeSpan time;

        private DispatcherTimer timer;

        private RelayCommand startCommand;

        #endregion

        #region Construction / Destruction
        public TimerViewModel()
        {
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(1000);

            if (this.IsInDesignMode)
            {
                this.Hours = 10;
                this.Minutes = 30;
                this.Seconds = 50;
            }
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public int Hours
        {
            get { return hours; }
            set
            {
                if (hours != value)
                {
                    hours = value;
                    RaisePropertyChanged("Hours");
                }
            }
        }

        public int Minutes
        {
            get { return minutes; }
            set
            {
                if (minutes != value)
                {
                    minutes = value;
                    if (minutes > 59)
                    {
                        minutes = 59;
                    }
                    RaisePropertyChanged("Minutes");
                }
            }
        }

        public int Seconds
        {
            get { return seconds; }
            set
            {
                if (seconds != value)
                {
                    seconds = value;
                    if (minutes > 59)
                    {
                        seconds = 59;
                    }
                    RaisePropertyChanged("Seconds");
                }
            }
        }

        public ICommand StartCommand
        {
            get
            {
                if (this.startCommand == null)
                {
                    this.startCommand = new RelayCommand(OnStart);
                }
                return this.startCommand;
            }
        }


        #endregion

        #region Methods

        #endregion

        #region Events
        void timer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            time = time.Subtract(TimeSpan.FromSeconds(1));

            if (time > TimeSpan.Zero)
            {
                this.Hours = time.Hours;
                this.Minutes = time.Minutes;
                this.Seconds = time.Seconds;
            }
            else
            {
                this.Hours = 0;
                this.Minutes = 0;
                this.Seconds = 0;
                this.timer.Stop();
            }
        }

        protected void OnStart()
        {
            if (this.timer.IsEnabled)
            {
                this.timer.Stop();
            }
            else
            {
                time = new TimeSpan(this.Hours, this.Minutes, this.Seconds);

                DateTime now = DateTime.Now;
                startDate = now;


                this.timer.Start();
            }
        }
        #endregion


    }
}
